<template>
  <t-dialog
    :header="`${header}-项目终止`"
    :visible.sync="visible"
    :onClose="closeDialog"
    :closeOnOverlayClick="false"
    @confirm="onSubmit"
    :confirmBtn="{
        content: loading?'提交中...':'确认',
        theme: 'primary',
        loading: loading,
      }"
    width="60%"
    top="5%"
  >
    <div style="height: 65vh">
      <t-form ref="form" label-align="top" :data="formData" :colon="true" labelWidth="100px" :rules="rules">
        <t-form-item label="终止意见" name="opinion">
          <t-textarea v-model="formData.opinion" :autosize="{minRows: 8}"/>
        </t-form-item>
      </t-form>
    </div>
  </t-dialog>
</template>

<script>
export default {
  name: "add-edit-system",
  props:{
    header:{
      type: String,
      default: '',
    },
    objId:{
      type: [Number, String],
    }
  },
  data(){
    return {
      visible: true,
      loading: false,
      formData:{
        id: this.objId,
        opinion: ''
      },
      rules:{
        opinion: [
          { required: true, message: "终止意见不能为空" }
        ],
      }
    }
  },
  methods:{
    closeDialog() {
      this.$emit("update:visible", false);
    },
    onSubmit() {
      this.$refs.form.validate().then((validateResult) => {
        if (validateResult === true) {
          const confirmDia = this.$dialog.confirm({
            header: "系统提示",
            body: `是否确认终止项目名称为"${this.header}"的数据项？`,
            theme: "danger",
            onConfirm: ({e}) => {
              confirmDia.update({
                confirmBtn: {
                  loading: true,
                },
              })
              this.$api.kjxm.project.terminate(this.formData).then(() => {
                this.$message.success("终止成功");
                confirmDia.destroy();
                this.$emit("reload");
                this.$emit("update:visible", false);
              }).catch((e) => {
                confirmDia.update({
                  confirmBtn: {
                    loading: false,
                  },
                })
                this.$message.error(e.toString());
              });
            }
          });
        }else {
          const firstError = Object.values(validateResult)[0]?.[0]?.message;
          this.$message.error(firstError);
        }
      });
    },
  }
}
</script>

<style scoped>

</style>
